package com.example.qny01.controller;

import com.example.qny01.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qny01.pojo.CommonResult;
import qny01.pojo.Student;
import qny01.pojo.Teacher;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("Teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("selectAll")
    public CommonResult selectAll(){
        List<Teacher> teachers = teacherService.selectAll();
        System.out.println("调用了teacher selectAll方法");
        return new CommonResult(200,"查询教师成功",teachers);
    }

    @GetMapping("selectById/{id}")
    public CommonResult selectById(@PathVariable("id") Integer id){
        Teacher teacher = teacherService.selectById(id);
        if(teacher!=null){
            return new CommonResult<Teacher>(200,"查询成功",teacher);
        }else{
            return new CommonResult<Teacher>(444,"没有查询到该老师",null);
        }
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody Teacher teacher){
        System.out.println("调用了insert方法");
        int result = teacherService.insert(teacher);
        if(result>0){
            return new CommonResult(200,"添加老师成功",result);
        }else {
            return new CommonResult(444,"添加老师失败",null);
        }
    }

    @DeleteMapping("delete/{id}")
    public CommonResult delete(@PathVariable("id") Integer id){
        System.out.println("调用了delete方法");

        int delete = teacherService.delete(id);
        if(delete>0){
            return new CommonResult(200,"删除数据成功",delete);
        }else {
            return new CommonResult(444,"删除数据失败",null);
        }
    }

    /**
     * 更新操作
     * */
    @PutMapping("update")
    public CommonResult update(@RequestBody Teacher teacher){
        System.out.println("调用了update方法");
        int result = teacherService.update(teacher);
        if(result>0){
            return new CommonResult(200,"更新数据成功",result);
        }else {
            return new CommonResult(444,"更新数据失败",null);
        }
    }

}
